package org.example.wikwok.controller;

import io.jsonwebtoken.Claims;
import org.example.wikwok.common.JwtProperties;
import org.example.wikwok.common.JwtUtil;
import org.example.wikwok.pojo.Result;
import org.example.wikwok.pojo.Video;
import org.example.wikwok.service.InteractionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

//处理点赞、评论和分享的HTTP请求。
@RestController
@RequestMapping("/interaction")
public class InteractionController {
    @Autowired
    private InteractionService interactionService;

    @RequestMapping(value = "/follow", method = RequestMethod.POST)
    public Result<Object> Follow(String token, String userId) {
        if (token == null || token.isEmpty()) {
            return Result.buildFailure(8888);
        }
        Claims claims = JwtUtil.parseJWT(JwtProperties.getUserSecretKey(), token);
        int followerId = Integer.parseInt(claims.get("userId").toString());
        boolean success = interactionService.follow(followerId, userId);
        if (success) {
            return Result.ok(null, 1201);
        } else {
            return Result.buildFailure(1202);
        }
    }

    @RequestMapping(value = "/unfollow", method = RequestMethod.POST)
    public Result<Object> Unfollow(String token, String userId) {
        if (token == null || token.isEmpty()) {
            return Result.buildFailure(8888);
        }
        Claims claims = JwtUtil.parseJWT(JwtProperties.getUserSecretKey(), token);
        int followerId = Integer.parseInt(claims.get("userId").toString());
        boolean success = interactionService.unfollow(followerId, userId);
        if (success) {
            return Result.ok(null, 1211);
        } else {
            return Result.buildFailure(1212);
        }
    }

    @RequestMapping(value = "/like", method = RequestMethod.POST)
    public Result<Object> Like(String token, String videoId) {
        if (token == null || token.isEmpty()) {
            return Result.buildFailure(8888);
        }
        Claims claims = JwtUtil.parseJWT(JwtProperties.getUserSecretKey(), token);
        int likerId = Integer.parseInt(claims.get("userId").toString());
        boolean success = interactionService.like(likerId, videoId);
        if (success) {
            return Result.ok(null, 2301);
        } else {
            return Result.buildFailure(2302);
        }
    }

    @RequestMapping(value = "/unlike", method = RequestMethod.POST)
    public Result<Object> Unlike(String token, String videoId) {
        if (token == null || token.isEmpty()) {
            return Result.buildFailure(8888);
        }
        Claims claims = JwtUtil.parseJWT(JwtProperties.getUserSecretKey(), token);
        int likerId = Integer.parseInt(claims.get("userId").toString());
        boolean success = interactionService.unlike(likerId, videoId);
        if (success) {
            return Result.ok(null, 2311);
        } else {
            return Result.buildFailure(2312);
        }
    }

    @RequestMapping(value = "/collect", method = RequestMethod.POST)
    public Result<Object> Collect(String token, String videoId) {
        if (token == null || token.isEmpty()) {
            return Result.buildFailure(8888);
        }
        Claims claims = JwtUtil.parseJWT(JwtProperties.getUserSecretKey(), token);
        int collectorId = Integer.parseInt(claims.get("userId").toString());
        boolean success = interactionService.collect(collectorId, videoId);
        if (success) {
            return Result.ok(null, 2201);
        } else {
            return Result.buildFailure(2202);
        }
    }

    @RequestMapping(value = "/uncollect", method = RequestMethod.POST)
    public Result<Object> Uncollect(String token, String videoId) {
        if (token == null || token.isEmpty()) {
            return Result.buildFailure(8888);
        }
        Claims claims = JwtUtil.parseJWT(JwtProperties.getUserSecretKey(), token);
        int collectorId = Integer.parseInt(claims.get("userId").toString());
        boolean success = interactionService.uncollect(collectorId, videoId);
        if (success) {
            return Result.ok(null, 2211);
        } else {
            return Result.buildFailure(2212);
        }
    }

    @RequestMapping(value = "/comment", method = RequestMethod.POST)
    public Result<Object> Comment(String token, String videoId, String content) {
        if (token == null || token.isEmpty()) {
            return Result.buildFailure(8888);
        }
        Claims claims = JwtUtil.parseJWT(JwtProperties.getUserSecretKey(), token);
        int commenterId = Integer.parseInt(claims.get("userId").toString());
        boolean success = interactionService.comment(commenterId, videoId, content);
        if (success) {
            return Result.ok(null, 3201);
        } else {
            return Result.buildFailure(3202);
        }
    }
@RequestMapping(value="/uncomment",method=RequestMethod.POST)
    public Result<Object> Uncomment(String token,String videoId,String commentId){
        if(token==null||token.isEmpty()){
            return Result.buildFailure(8888);
        }
        Claims claims = JwtUtil.parseJWT(JwtProperties.getUserSecretKey(), token);
        int commenterId = Integer.parseInt(claims.get("userId").toString());
        boolean success = interactionService.uncomment(commenterId, videoId, commentId);
        if(success){
            return Result.ok(null,3211);
        }else{
            return Result.buildFailure(3212);
        }
    }
   @RequestMapping(value="/secendComment",method=RequestMethod.POST)
    public Result<Object> SecendComment(String token,String commentId,String content){
        if(token==null||token.isEmpty()){
            return Result.buildFailure(8888);
        }
        Claims claims = JwtUtil.parseJWT(JwtProperties.getUserSecretKey(), token);
        int commenterId = Integer.parseInt(claims.get("userId").toString());
        boolean success = interactionService.secendComment(commenterId, commentId, content);
        if(success){
            return Result.ok(null,3301);
        }else{
            return Result.buildFailure(3302);
        }
    }
    @RequestMapping(value="/unsecendComment",method=RequestMethod.POST)
    public Result<Object> UnsecendComment(String token,String commentId){
        if(token==null||token.isEmpty()){
            return Result.buildFailure(8888);
        }
        Claims claims = JwtUtil.parseJWT(JwtProperties.getUserSecretKey(), token);
        int commenterId = Integer.parseInt(claims.get("userId").toString());
        boolean success = interactionService.unsecendComment(commenterId, commentId);
        if(success){
            return Result.ok(null,3311);
        }else{
            return Result.buildFailure(3312);
        }
    }
}
